#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct list_node
{
	unsigned int data;
	//struct list_node *prev;
	struct list_node *next;
};


struct list_node *create_list()
{
	struct list_node *l = malloc(sizeof(struct list_node));
	if(l == NULL){
		printf("malloc failed\n");
		return (-1);
	}
	memset(l, 0, sizeof(struct list_node));

	l->data = 0;
	l->next = NULL;

	return l;
}

struct list_node *create_node()
{
	struct list_node *n = malloc(sizeof(struct list_node));
	if(n == NULL){
		printf("create_node malloc failed\n");
		exit(-1);
	}

	memset(n, 0, sizeof(struct list_node));
    n->next = NULL;

	return n;
}

void list_insert_after(struct list_node *l, struct list_node *n)
{
	n->next = l->next;    
	l->next = n;
}

struct list_node *list_reverse(struct list_node *head)
{
	struct list_node *second;
	struct list_node *new_head;
	
	if(head == NULL || head->next == NULL){
		return head;
	}

	second = head->next;
	new_head = list_reverse(second);
	second->next = head;
	head->next = NULL;

	return new_head;
}

int list_del(struct list_node *head)
{
	while(!head->next)
	{
		list_remove_after(head);
	}

	free(head);
}

int list_remove_after(struct list_node *l)
{
	struct list_node *n;
	if(l->next == NULL)
		return 0;

	n = l->next;
	l->next = n->next;
	free(n);
}

int main()
{
	unsigned int i;
	struct list_node *head;
	struct list_node *n;
	struct list_node *new_head;
	/*1. 创建头节点*/
	head = create_list();
	
	for(i=1; i < 10; i++)
	{
		n = create_node();
		n->data = i;
		list_insert_after(head, n);
	}
	
	n = head;
	while(n)
	{
		printf("%d\n", n->data);
		n = n->next;
	}
	
	new_head = list_reverse(head);

	n = new_head;
	while(n)
	{
		printf("%d\n", n->data);
		n = n->next;
	}

	//list_del(head);
	list_del(new_head);
	return 0;
}
